package com.tank.ui;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.texture.Texture;
import com.tank.components.BulletLevelComponent;
import com.tank.constant.Config;
import com.tank.constant.GameType;
import javafx.collections.ObservableList;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.layout.HBox;
import javafx.scene.layout.TilePane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.Text;

import java.util.List;

public class InfoPane extends VBox {
    public InfoPane() {
        setSpacing(50);
        setStyle("-fx-background-color: lightgray");
        setPrefSize((Config.BOARD_CELL_COUNT + 1) * Config.CELL_SIZE, FXGL.getAppHeight());
        setTranslateX((Config.GROUND_CELL_COUNT - 1) * Config.CELL_SIZE);
        setAlignment(Pos.TOP_CENTER);
        TilePane enemyBoard = new TilePane();
        setMargin(enemyBoard, new Insets(30, 0, 0, 0));
        enemyBoard.setMaxWidth(25 * 2 + 15);
        enemyBoard.setHgap(10);
        enemyBoard.setVgap(8);
        enemyBoard.setPrefHeight(25 * 10 + 6 * 9 + 15);
        enemyBoard.setAlignment(Pos.CENTER);
//        enemyBoard.setStyle("-fx-border-color: red");
        for (int i = 0; i < Config.MAX_ENEMY_AMOUNT; ++i) {
            enemyBoard.getChildren().add(FXGL.texture("ui/enemy_pre.png"));
        }
        FXGL.getip(Config.ENEMY_AMOUNT).addListener((observable, oldValue, newValue) -> {
            ObservableList<Node> children = enemyBoard.getChildren();
            for (int i = children.size() - 1; i >= Config.MAX_ENEMY_AMOUNT - newValue.intValue(); --i) {
                children.get(i).setVisible(false);
            }
        });
        getChildren().add(enemyBoard);

        Texture levelFlag = FXGL.texture("ui/level_flag.png");
        Text level = new Text(FXGL.geti(Config.MAP_LEVEL) + "");
        level.setFont(Font.font(26));
        HBox levelBox = new HBox(levelFlag, level);
        levelBox.setAlignment(Pos.BOTTOM_CENTER);
        levelBox.setMaxWidth(80);
        getChildren().add(levelBox);

        HBox bulletLevel = new HBox();
        bulletLevel.setPadding(new Insets(0, 0, 0, Config.CELL_SIZE));
        for (int i = 0; i <= Config.MAX_TANK_LEVEL; ++i) {
            Texture bullet = FXGL.texture("ui/bullet_level.png");
            bullet.setVisible(i == 0);
            bulletLevel.getChildren().add(bullet);
        }
        List<Entity> players = FXGL.getGameWorld().getEntitiesByType(GameType.PLAYER);
        if (!players.isEmpty()) {
            players.get(0).getComponent(BulletLevelComponent.class).valueProperty().addListener((observable, oldValue, newValue) -> {
                for (int i = 0; i <= newValue.intValue() && i < bulletLevel.getChildren().size(); ++i) {
                    bulletLevel.getChildren().get(i).setVisible(true);
                }
            });
        }
        getChildren().add(bulletLevel);
    }
}
